<?php
namespace Shop\Controller;

use Common\Controller\AdminbaseController;

class FloorController extends AdminbaseController
{

    protected $data_model;

    function _initialize()
    {
        parent::_initialize();
        $this->data_model = D("Floor");
    }

    public function index()
    {
        $floors = $this->data_model->order("`order` ASC")->select();
        $this->assign("floors", $floors);
        $this->display();
    }

    public function add()
    {
        $sm_floors = $this->data_model->db(1,"DB_CONFIG_SM")->query("select areaid,areaname from destoon_area");
        $this->assign("sm_floors",$sm_floors);
        $this->display();
    }

    public function add_post()
    {
        if (IS_POST) {
            $res = upload('attac');
            $data = $_POST;
            if ($res){
                $data['attachmentID'] = $res;
            }
            if ($this->data_model->create() !== false) {
                if ($this->data_model->add($data) !== false) {
                    $this->success("添加成功！", U("Floor/index"));
                }else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->data_model->getError());
            }
        }
    }

    public function delete()
    {
        if (I('get.id')) {
            $id = I('get.id');
            $res = $this->data_model->delete($id);
            if ($res) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
        if (I('post.ids')) {
            $ids = I('post.ids');
            $res = $this->data_model->where(array(
                "id" => array(
                    "in",
                    $ids
                )
            ))->delete();
            if ($res) {
                $this->success("批量删除成功！");
            } else {
                $this->error("批量删除失败！");
            }
        }
    }

    public function edit()
    {
        $sql = "select a.*,b.savepath,b.savename from ds_floor a left join ds_attachment b on a.attachmentID = b.id where a.id = ".I('get.id')." limit 1";
        $floor = $this->data_model->query($sql);
        $this->assign("floor", $floor[0]);
        $sm_floors = $this->data_model->db(1,"DB_CONFIG_SM")->query("select areaid,areaname from destoon_area");
        $this->assign("sm_floors",$sm_floors);
        $this->display();
    }

    public function edit_post()
    {
        $res = upload('attac');
        $floor_arr = $_POST;
        if ($res){
            $floor_arr['attachmentID'] = $res;
        }
        $res = $this->data_model->save($floor_arr);
        if ($res) {
            $this->success("编辑成功！");
        } else {
            $this->error($this->data_model->getError());
        }
    }
}
?>